import { axiosInstance } from "./config";
import { axiosInstances  } from "./configs";
import { AxiosResponse } from 'axios'

import {
    URL_FIND_MAIN,
    URL_Cities,
    URL_LOCAL,
    URL_SEARCH_SWIPERS,
    URL_SEARCH_HOTKEY,
    URL_SEARCH_SUGGEST,
    URL_SEARCH_RESULT,
    URL_RESORT_AD,
    URL_RESORT_HOT_CITY,
    URL_RESORT_SHOP_ITEMS,
    URL_GOOD_STUFF_LIST,
    URL_GOOD_STUFF_TABS,
    URL_SPECIAL_SALE_ITEMS
} from './url'

import { findSwiperListType,swiperType } from '@/models/swiper'
import { rankList } from '@/models/search'

export const getFindMainRequest=
    ()=>axiosInstance.get(URL_FIND_MAIN)

export const getCitiesRequest=
    ()=>axiosInstance.get(URL_Cities)

export const getLocalRequest=
    ()=>axiosInstance.get(URL_LOCAL)

// qunaer
export const getResortAdRequest =
    () => axiosInstance.get(URL_RESORT_AD)

export const getResortHotCityRequest =
    () => axiosInstance.get(URL_RESORT_HOT_CITY)

export const getResortShopItemsRequest =
    () => axiosInstance.get(URL_RESORT_SHOP_ITEMS)

export const getGoodStuffListRequest =
    (page: number ,key: string, date: number) => 
        axiosInstance.get(`${URL_GOOD_STUFF_LIST}?page=${page}&channelEnId=${key}&pushRS=${date}`)

export const getGoodStuffTabsRequest =
    () => axiosInstance.get(URL_GOOD_STUFF_TABS)

export const getSpecialSaleitemsRequest =
    () => axiosInstance.get(URL_SPECIAL_SALE_ITEMS)

// search
export const getSearchSwipersRequest = ():Promise<swiperType[]> => {
    const promise = new Promise<swiperType[]>((resolve,reject) => {
        try{
            axiosInstance.get(URL_SEARCH_SWIPERS).then(
                response => resolve(mapSwiperList(response))
            )
        } catch(e) {
            reject(e)
        }
    })
    return promise
}
//将AxiosResponse类型转为swiperType[]类型
const mapSwiperList = 
    ({data}:AxiosResponse<swiperType[]>):swiperType[] => 
        data.map(item => ({
            id:item.id,
            img:item.img
        }))

// 获取搜索排行榜热词
export const getSearchHotKeyRequest = ():Promise<rankList[]> => {
    const promise = new Promise<rankList[]>((resolve,reject) => {
        try{
            axiosInstance.get(URL_SEARCH_HOTKEY).then(
                response => resolve(mapHotkeyList(response))
            )
        } catch(e) {
            reject(e)
        }
    })
    return promise
}
const mapHotkeyList= 
    ({data}:AxiosResponse):rankList[] => 
        data.rankList.map((item:rankList) => ({
            listName:item.listName,
            sortNo:item.sortNo,
            hotLists:item.hotLists
    }))

// 获取搜索建议列表 
export const getSearchSuggestRequest = 
        (w:string) => {
            let newUrl = URL_SEARCH_SUGGEST.replace('{w}',w)
            return axiosInstance.get(newUrl)
        }

// 搜索结果数据
export const getSearchResultRequest = 
    (w:string) => {
        let newUrl = URL_SEARCH_RESULT.replace('{w}',w)
        // console.log(newUrl)
        return axiosInstance.get(newUrl)
    }
